package com.kcht.np.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcht.np.common.exception.ParameterException;
import com.kcht.np.entity.SysRole;
import com.kcht.np.service.SysRoleService;
import com.kcht.np.util.JsonResult;
import com.kcht.np.vo.page.QueryParameter;
import com.kcht.np.vo.RoleRelatedPermissionVo;
import com.kcht.np.vo.role.RoleCanCreateInfo;
import com.kcht.np.vo.role.RoleCanModifyInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
  * <p>
  * 角色管理 控制器
  * </p>
  *
  * @Package:        com.kcht.np.mp.controller
  * @Description:    角色管理 控制器
  * @Author:         liuqi
  * @CreateDate:     2020/3/31 11:27
 */

@Slf4j
@Api(tags = "[ 权限管理 ] 角色管理")
@SuppressWarnings("all")
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    /*--------------查询操作--------------------*/
    /*拥有管理用户权限可执行的查询操作*/

    @ApiOperation(value = "查询所有角色的基本信息", notes = "有管理用户权限的即可执行此操作")
    @GetMapping("/all")
    @PreAuthorize("hasAuthority('pre_role')||hasAnyAuthority('pre_user')")
    public JsonResult<List<SysRole>> querySysRoles() {
        return JsonResult.success(roleService.list());
    }
    @ApiOperation(value = "分页,获取角色详情", notes = "分页 查询所有，获取角色详情")
    @GetMapping("/info/page")
    @PreAuthorize("hasAuthority('pre_role')")
    public JsonResult<Page<SysRole>> pagingQueryRoles(QueryParameter parameter,BindingResult br) {
        IPage<SysRole> page = new Page<SysRole>(parameter.getPageNum(), parameter.getPageSize());
        IPage<SysRole> iPage = roleService.page(page);
        return JsonResult.success(iPage.getPages(),iPage.getTotal(), iPage.getRecords());
    }


    @ApiOperation(value = "查询角色详情，包括权限列表", notes = "根据Id查询")
    @GetMapping("/info/{rid}")
    @PreAuthorize("hasAuthority('pre_role')||hasAnyAuthority('pre_user')")
    public JsonResult queryRichRole(@PathVariable Long rid) {
        try{
            Map<String, Object> map = roleService.queryRichRole(rid);
            return JsonResult.success(map);
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }

    /*-----------------创建操作--------------*/
    @ApiOperation(value = "创建角色", notes = "创建角色")
    @PostMapping
    @PreAuthorize("hasAuthority('pre_role:create')")
    public JsonResult<SysRole> create(@RequestBody RoleCanCreateInfo info) {
        roleService.save(info);
        return JsonResult.success();
    }

    /*----------------修改操作-------*/
    @ApiOperation(value = "修改角色权限", notes = "修改角色权限,会删除之前的权限信息。")
    @PutMapping("/update/permissions")
    @PreAuthorize("hasAuthority('pre_role:update:permissions')")
    public JsonResult updateRolePermissions(@RequestBody @Validated RoleRelatedPermissionVo data, BindingResult br) {
        try{
            roleService.grantPermissions(data.getRid(), data.getPermissions());
            return JsonResult.success();
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }

    @ApiOperation(value = "修改角色", notes = "修改角色基本信息")
    @PutMapping
    @PreAuthorize("hasAuthority('pre_role:update')")
    public JsonResult update(@RequestBody RoleCanModifyInfo info) {
        try{
            roleService.update(info);
            return JsonResult.success();
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }

    /*------------删除操作----------*/
    @ApiOperation(value = "删除角色",notes = "删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('pre_role:delete')")
    public JsonResult<String> delete(@PathVariable("id") Long id) {
        try{
            roleService.delete(id);
            return JsonResult.success();
        }catch (ParameterException e){
            return JsonResult.result(400,e.getMessage());
        }
    }

}
